var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "maishu-jueying", "components/component-container", "common", "components/common/base-component", "./local-service", "components/menu/component", "components/categories/component", "components/carousel/component", "components/product-info/component", "components/product-info/product-info-footer", "components/member/component", "components/shopping-cart/component", "components/shopping-cart/shopping-cart-footer", "components/shopping-cart/shopping-cart-header", "components/store-coupons/component"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const maishu_jueying_1 = require("maishu-jueying");
    const component_container_1 = require("components/component-container");
    const common_1 = require("common");
    const base_component_1 = require("components/common/base-component");
    const local_service_1 = require("./local-service");
    const component_1 = require("components/menu/component");
    const component_2 = require("components/categories/component");
    const component_3 = require("components/carousel/component");
    const component_4 = require("components/product-info/component");
    const product_info_footer_1 = require("components/product-info/product-info-footer");
    const component_5 = require("components/member/component");
    const component_6 = require("components/shopping-cart/component");
    const shopping_cart_footer_1 = require("components/shopping-cart/shopping-cart-footer");
    const shopping_cart_header_1 = require("components/shopping-cart/shopping-cart-header");
    const component_7 = require("components/store-coupons/component");
    let BODY_TAG = "section";
    let FOOTER_TAG = "footer";
    let HEADER_TAG = "header";
    function defaultHeaderData() {
        return {
            type: HEADER_TAG,
            children: [{
                    type: component_container_1.ComponentContainer.name,
                    props: {
                        attr: { movable: false, container: false, showHandler: false, resize: false },
                        id: common_1.guid(),
                        text: "请拖拉控件到这里",
                    }
                }]
        };
    }
    function defaultFooterData() {
        return {
            type: FOOTER_TAG,
            children: [{
                    type: component_container_1.ComponentContainer.name,
                    props: {
                        attr: { movable: false, container: false, showHandler: false, resize: false, },
                        id: common_1.guid(),
                        text: "请拖拉控件到这里",
                    }
                }]
        };
    }
    function defaultBodyData() {
        return {
            type: BODY_TAG,
            children: [{
                    type: component_container_1.ComponentContainer.name,
                    props: {
                        attr: { movable: false, container: false, showHandler: false, resize: false },
                        id: common_1.guid(),
                        text: "请拖拉控件到这里",
                    }
                }]
        };
    }
    function emptyPageData() {
        return {
            type: maishu_jueying_1.MasterPage.name,
            children: [defaultBodyData()]
        };
    }
    exports.defaultPageDatas = {
        home() {
            return emptyPageData();
        },
        empty() {
            return emptyPageData();
        },
        member() {
            let pageData = emptyPageData();
            addComponentDataToBody(pageData, {
                type: component_5.MemberPage.name, props: { selected: true }
            });
            return pageData;
        },
        menu() {
            let pageData = emptyPageData();
            addComponentDataToFooter(pageData, {
                type: component_1.Menu.name, props: { selected: true }
            });
            return pageData;
        },
        shoppingCart() {
            let pageData = emptyPageData();
            addComponentDataToHeader(pageData, {
                type: shopping_cart_header_1.ShoppingCartHeader.name
            });
            addComponentDataToBody(pageData, {
                type: component_6.ShoppingCart.name, props: { selected: true }
            });
            addComponentDataToFooter(pageData, {
                type: shopping_cart_footer_1.ShoppingCartFooter.name
            });
            return pageData;
        },
        categories() {
            let pageData = emptyPageData();
            addComponentDataToBody(pageData, {
                type: component_2.Categories.name,
                props: { selected: true }
            });
            return pageData;
        },
        product() {
            let pageData = emptyPageData();
            addComponentDataToBody(pageData, {
                type: component_3.Carousel.name
            });
            addComponentDataToBody(pageData, {
                type: component_4.ProductInfo.name,
                props: { selected: true }
            });
            addComponentDataToFooter(pageData, {
                type: product_info_footer_1.ProductInfoFooter.name,
            });
            return pageData;
        },
        storeCoupons() {
            let pageData = emptyPageData();
            addComponentDataToBody(pageData, {
                type: component_7.StoreCoupons.name, props: { selected: true }
            });
            return pageData;
        }
    };
    let localService = base_component_1.createService(local_service_1.LocalService);
    exports.pageDatas = {
        home: function () {
            return __awaiter(this, void 0, void 0, function* () {
                return getPageDataRecord("home");
            });
        },
        member: function () {
            return __awaiter(this, void 0, void 0, function* () {
                return getPageDataRecord("member");
            });
        },
        menu: function () {
            return __awaiter(this, void 0, void 0, function* () {
                return getPageDataRecord("menu");
            });
        },
        shoppingCart: function () {
            return __awaiter(this, void 0, void 0, function* () {
                return getPageDataRecord("shoppingCart");
            });
        },
        categories: function () {
            return __awaiter(this, void 0, void 0, function* () {
                return getPageDataRecord("categories");
            });
        },
        product() {
            return getPageDataRecord("product");
        },
        storeCoupons() {
            return getPageDataRecord("storeCoupons");
        }
    };
    function getPageDataRecord(name) {
        return __awaiter(this, void 0, void 0, function* () {
            let reocrd = yield localService.getPageDataRecordByName(name);
            if (reocrd == null) {
                let pageData = exports.defaultPageDatas[name]();
                return { pageData, name, type: "page" };
            }
            return reocrd;
        });
    }
    function getBodyContainerId(componentData) {
        return getPartyContainerId(BODY_TAG, componentData);
    }
    function getFooterContainerId(componentData) {
        return getPartyContainerId(FOOTER_TAG, componentData);
    }
    function getHeaderContainerId(componentData) {
        return getPartyContainerId(HEADER_TAG, componentData);
    }
    function getPartyContainerId(tagName, componentData) {
        let stack = new Array();
        stack.push(componentData);
        while (stack.length > 0) {
            let item = stack.pop();
            if (item.type == tagName) {
                let containerData = item.children.filter(o => o.type == component_container_1.ComponentContainer.name)[0];
                if (containerData)
                    return containerData.props.id;
                return null;
            }
            if (item.children)
                stack.push(...item.children);
        }
    }
    exports.getPartyContainerId = getPartyContainerId;
    function addComponentDataToHeader(pageData, item) {
        let headerData = pageData.children.filter(o => o.type == HEADER_TAG)[0];
        if (headerData == null) {
            pageData.children.push(defaultHeaderData());
        }
        let containerId = getHeaderContainerId(pageData);
        console.assert(containerId != null);
        item.props = item.props || {};
        item.props.parentId = containerId;
        pageData.children.push(item);
    }
    exports.addComponentDataToHeader = addComponentDataToHeader;
    function addComponentDataToBody(pageData, item) {
        let containerId = getBodyContainerId(pageData);
        console.assert(containerId != null);
        item.props = item.props || {};
        item.props.parentId = containerId;
        pageData.children.push(item);
    }
    exports.addComponentDataToBody = addComponentDataToBody;
    function addComponentDataToFooter(pageData, item) {
        let footerData = pageData.children.filter(o => o.type == FOOTER_TAG)[0];
        if (footerData == null) {
            pageData.children.push(defaultFooterData());
        }
        let containerId = getFooterContainerId(pageData);
        console.assert(containerId != null);
        item.props = item.props || {};
        item.props.parentId = containerId;
        pageData.children.push(item);
    }
    exports.addComponentDataToFooter = addComponentDataToFooter;
    function removeChildComponentData(pageData, type) {
        pageData.children = pageData.children.filter(o => o.type != type);
        return pageData;
    }
    exports.removeChildComponentData = removeChildComponentData;
    function removeComponentDataFromFooter(pageData, type) {
        let footerData = pageData.children.filter(o => o.type == FOOTER_TAG)[0];
        if (footerData == null) {
            return;
        }
        let containerId = getFooterContainerId(pageData);
        pageData.children = pageData.children.filter(o => !(o.type == type && o.props.parentId == containerId));
        //===========================================================================================
        // 如果 footer 没有子组件，移除 footer
        let footerChildren = pageData.children.filter(o => o.props.parentId == containerId);
        if (footerChildren.length == 0) {
            pageData.children = pageData.children.filter(o => o.props.id != footerData.props.id);
        }
        //===========================================================================================
        return pageData;
    }
    exports.removeComponentDataFromFooter = removeComponentDataFromFooter;
    function findChildComponentData(componentData, type) {
        let stack = [...(componentData.children || [])];
        while (stack.length > 0) {
            let item = stack.shift();
            if (item.type == type)
                return item;
            stack.push(...item.children || []);
        }
        return null;
    }
    exports.findChildComponentData = findChildComponentData;
    function findFooterComponentData(componentData) {
        let footerData = findChildComponentData(componentData, FOOTER_TAG);
        return footerData;
    }
    exports.findFooterComponentData = findFooterComponentData;
});
